package controller.v2;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import model.JsonVO;
import model.MemberVO;
import model.OrderTimer;
import model.OrderVO;
import service.v2.OrderCancelService;
import service.v2.TimerService;
import service.v2.UorderService;
import util.PackageUtil;

@Controller
@RequestMapping("/v2/timer")
public class TimerController {
	
	@Autowired
	private TimerService timerService;
	
	@Autowired
	private OrderCancelService orderCancelService;
	
	@Autowired
	private UorderService uorderService;
	
	/*
	 * 	order_timer_id 定时器ID
	 * 	order_id       订单ID
	 * 	order_back_id  退货ID 
	 * 	timer_state    0 订单 1 退货
	 * 	senconds       秒数
	 * 	type           定时器TYPE
	 */
	
	//接收定时器回调
	@ResponseBody
	@RequestMapping(value="/recieveTimer",method=RequestMethod.POST) 
	public Object  recieveTimer(@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,
			HttpServletRequest request, HttpServletResponse response, MemberVO memberVO,
			String data) throws Exception{
		if(data!=null){
			boolean state = timerService.recieveTimer(data);
			if(state==true){
				return PackageUtil.packageParams(200,"successs","","");
			}else{
				return PackageUtil.packageParams(201,"","","");
			}
			
		}else{
			return PackageUtil.packageParams(201,"","","");
		}

	}
	
	//发货时7点收货接口
	@ResponseBody
	@RequestMapping(value="/orderSendTimer",method=RequestMethod.POST) 
	public void  orderPayTimer(@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,
			HttpServletRequest request, HttpServletResponse response, MemberVO memberVO,
			String order_timer_id,String order_id, String order_back_id,String timer_state,String senconds,String type){
		timerService.orderPayTimer(order_timer_id, order_id, order_back_id, timer_state, senconds, type);
	}
	
	//js 获取所有 定时器
	@ResponseBody
	@RequestMapping(value="/getAllTimer",method=RequestMethod.GET) 
	public Object  getAllTimer(@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,
			HttpServletRequest request, HttpServletResponse response, MemberVO memberVO){
		
		List<OrderTimer> list = timerService.getAllTimer();
		if(list==null){
			list =  new ArrayList<OrderTimer>();
		}
		return PackageUtil.packageParams(200,"successs",list,"");
	}
	
	@ResponseBody
	@RequestMapping(value="/test",method=RequestMethod.POST) 
	public void  test(String a ) throws Exception{
		System.out.println("))))))))))))))))))))))"+a);

	}
	
	/**
	 * 处理后台定时器
	 * 7天收货
	 * 1小时未处理订单
	 * @throws Exception 
	 */
	@ResponseBody
	@RequestMapping(value="/dealAdminOrderTimer",method=RequestMethod.POST) 
	public Object  dealAdminOrderTimer(@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,
			HttpServletRequest request, HttpServletResponse response) throws Exception{
		uorderService.dealPersonTimerBackOfiice();
		return PackageUtil.packageParams(200,"successs","","");
	}
}
